package com.ysd.twocrm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ysd.twocrm.entity.UserRoles;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

//用户角色mapper
public interface UserRolesMapper extends BaseMapper<UserRoles> {
    //查询角色名称的角色id
    @Select(value = "select roleId from roles where roleName=#{roleName}")
    Integer getAllRoleNameById(String roleName);

     //根据用户id和角色id来删除
    @Delete(value = "delete from userroles where roleId=#{roleid} and userId=#{userid}")
    Integer delUsersRolesById(Integer userid,Integer roleid);
    //根据用户id和角色
    @Select(value = "select * from userroles where roleid in(select roleId from roles where roleName=#{roleName}) and userId=#{userid}")
    UserRoles findWeight(Integer userid,String roleName);
    //根据id来修改权重值
    @Update(value = "update userroles set userRolesWeight=#{userRolesWeight} where userRolesId=#{userRolesId}")
    Integer exitWeight(Integer userRolesId,Integer userRolesWeight);
    //根据用户id来删除当前用户的角色
    @Delete(value = "delete from userroles where userid=#{userId}")
    Integer delUsersRole(Integer userId);
    //根据角色id来查询是否存在在用户角色表
    @Select(value = "select * from userroles where roleid=#{roleId}")
    List<UserRoles> userroleiflist(Integer roleId);
    //根据角色id来删除
@Delete(value = "delete from userroles where roleId=#{roleId}")
    Integer delUserRolesByRoleId (Integer roleId);
//测试中奖
@Select(value = "select u1.* from userroles u1 inner join roles r1 on u1.roleid=r1.roleid where r1.rolename='咨询师'  and u1.userid in(select userId from userchecks where usercheckstatus=1 and usercheckintime like concat(#{time},'%') ) order by userrolesweight desc")
    List<UserRoles> USER_ROLES(String time);
//根据用户id来让当前角色的权重值+10
    @Update(value = "update userroles set userrolesweight=userrolesweight+10 where userid=#{userId}")
    Integer updateWeightByUserId(Integer userId);
    //查询出当前没有业绩，也就是当前没有添加过学生的咨询师
  @Select(value = "select * from userroles where roleId in (select roleId from roles where roleName='咨询师') and userrolesid !=(select userrolesid from userroles where userid in(select userid from logs where operation = '咨询师添加一名学生' and createtime like concat(#{date},'%')) and roleid =(select roleid from roles where rolename ='咨询师'))")
    List<UserRoles> findAllByStudentLogs(String date);


}
